package cocoaas3.Titanium.UI{	
	import jp.progression.casts.*;import jp.progression.commands.display.*;import jp.progression.commands.lists.*;import jp.progression.commands.managers.*;import jp.progression.commands.media.*;import jp.progression.commands.net.*;import jp.progression.commands.tweens.*;import jp.progression.commands.*;import jp.progression.data.*;import jp.progression.events.*;import jp.progression.loader.*;import jp.progression.*;import jp.nium.core.debug.Logger;import jp.nium.utils.*;import caurina.transitions.Tweener;import caurina.transitions.properties.*;

	import flash.display.*;
	import flash.net.*;
	import flash.geom.*;
	import flash.events.*;
	import flash.media.*;

	import org.osflash.signals.*;


	public class WebView extends Sprite{

		private var webView:StageWebView = new StageWebView();
		public var url:String;
		public var complete_sig:Signal = new Signal();
		public var locationChange_sig:Signal = new Signal();
		public var locationChanging_sig:Signal = new Signal();

		public function WebView(){
			addEventListener( Event.ADDED_TO_STAGE, onAddedToStage );
			addEventListener( Event.REMOVED_FROM_STAGE, onRemovedFromStage );

			// getRectを正確に出すために、背景を配置
			var bg_mc:Shape = new Shape();
			bg_mc.graphics.beginFill( 0x333333, 1 );
			bg_mc.graphics.drawRect( 0, 0, 1, 1 );
			this.addChild( bg_mc );
		}

		private function onAddedToStage(e){
			addEventListener( Event.ENTER_FRAME, _onEnterFrame );

			webView.stage = CastDocument.stage;
			webView.addEventListener( Event.COMPLETE, _onPageLoadComplete );
			webView.addEventListener( LocationChangeEvent.LOCATION_CHANGE, _onLocationChange );
			webView.addEventListener( LocationChangeEvent.LOCATION_CHANGING, _onLocationChanging );
			webView.loadURL( url );
		}
		private function onRemovedFromStage(e){
			Logger.info( "WebView onRemovedFromStage" )
			removeEventListener( Event.ENTER_FRAME, _onEnterFrame );
			webView.removeEventListener( Event.COMPLETE, _onPageLoadComplete );
			webView.removeEventListener( LocationChangeEvent.LOCATION_CHANGE, _onLocationChange );
			webView.removeEventListener( LocationChangeEvent.LOCATION_CHANGING, _onLocationChanging );
			webView.stage = null;
			webView = null;
		}

		private function _onPageLoadComplete(e){
			Logger.info(e)
			complete_sig.dispatch();
		}
		private function _onLocationChange(e){
			Logger.info(e)
			locationChange_sig.dispatch();
		}
		private function _onLocationChanging(e){
			Logger.info(e)
			locationChanging_sig.dispatch();
		}

		private function _onEnterFrame(e){
			webView.viewPort = this.getRect( stage );
		}

		public function canGoBack():Boolean{
			return webView.isHistoryBackEnabled;
		}
		public function canGoForward():Boolean{
			return webView.isHistoryForwardEnabled;
		}
		public function goBack():void{
			webView.historyBack();
		}
		public function goForward():void{
			webView.historyForward();	
		}
	}
}



